package com.example.logintest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

public class LoginMainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {

    private static final String SHARE_KEY_PHONE = "phone";
    private static final String SHARE_KEY_PASSWORD = "password";

    private CheckBox cbRememberPassword;
    private Button btLogin;
    private boolean isRemember;
    private  SharedPreferences shareLogin;

    private EditText etPhone;
    private EditText etPassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_main);

        cbRememberPassword = findViewById(R.id.cb_remember);
        cbRememberPassword.setOnCheckedChangeListener(this);
        btLogin = findViewById(R.id.bt_login);
        btLogin.setOnClickListener(this);
        shareLogin = getSharedPreferences("share_login", MODE_PRIVATE);
        etPhone = findViewById(R.id.ev_phone);
        etPassword = findViewById(R.id.ev_password);

        etPhone.setText(shareLogin.getString(SHARE_KEY_PHONE, ""));
        etPassword.setText(shareLogin.getString(SHARE_KEY_PASSWORD, ""));
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        if (compoundButton.getId() == R.id.cb_remember){
            isRemember = isChecked;
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.bt_login){
            if (isRemember){
                SharedPreferences.Editor edit = shareLogin.edit();
                edit.putString(SHARE_KEY_PHONE, etPhone.getText().toString());
                edit.putString(SHARE_KEY_PASSWORD, etPassword.getText().toString());
                edit.commit();
            }
        }
    }
}